﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Utils
{
    /// <summary>
    /// 登录模块工具类
    /// </summary>
    public class MD5
    {
        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="str">密码</param>
        /// <returns></returns>
        public static string md5Encrypt(string str)
        {
            System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
            byte[] buffer = Encoding.UTF8.GetBytes(str);//将字符串转成字节数组
            byte[] byteArray = md5.ComputeHash(buffer);//调用加密方法
            StringBuilder sb = new StringBuilder();
            foreach (byte b in byteArray)//遍历字节数组
            {
                sb.Append(b.ToString("x2"));//将字节数组转成16进制的字符串。X表示16进制，2表示每个16字符占2位
            }
            return sb.ToString();
        }

    }
}
